package com.example.demo_homework2.controller;

import com.example.demo_homework2.bean.CmsMember;
import com.example.demo_homework2.common.Response;
import com.example.demo_homework2.service.CmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/member")
@Api(tags = "会员信息管理")
public class CmsMemberController {
    @Autowired
    private CmsMemberService memberService;
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "更新或插入会员信息",notes = "如果传参时指明了会员编号，执行更新操作<br/>"
            +"如果传参时没有指明会员编号，执行插入操作")
    public Response<String> saveOrUpdate(CmsMember cmsMember){
        memberService.saveOrUpdate(cmsMember);
        return Response.ok("操作成功");
    }

    @GetMapping ("/all")
    @ApiOperation(value = "获取所有会员信息",notes = "从数据库检索所有会员信息返回")
    public Response<List<CmsMember>> queryAll(){
        return Response.ok(memberService.getAllMeber());
    }

    @DeleteMapping("/deleteById")
    @ApiOperation(value = "删除会员信息",notes = "按会员编号删除会员信息")
    @ApiImplicitParam(name = "id",value = "会员编号")
    public Response<String> deleteById(String id){
        int num=memberService.deleteByid(id);
        return Response.ok("删除成功"+num+"条会员信息");
    }
    @DeleteMapping("/deleteByIds")
    @ApiOperation(value = "批量删除会员信息")
    public  Response<String> deleteByIds(String[] ids){
        String s=memberService.deleteByids(ids);
        return Response.ok(s);
    }
}
